<?php
namespace app\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\log\Logger;
use app\models\Article;
use app\models\Xiong;
use app\models\Options;
use app\models\Comment;
use app\models\Comment_live;
use app\models\User;
use app\models\Live;

/**
 * Site controller
 */
class BlogController extends Controller
{
    public $layout = false;
    public function actions()
    {
        return [ 
         'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
    ];
    }
    public function actionArticle()
    {
      $id = Yii::$app->request->get('id');   
      $comment = new Comment();
   // 接收评论并验证，true则存入数据库
       if($comment->load(\Yii::$app->request->post())&&$comment->validate())
     {        
      Yii::trace("test success");
                  $comment->comment_date = date("Y-m-d H:i:s",time());
                  $comment->article_id = $id;
                  $comment->save();    
                  \Yii::$app->session->setFlash('success','评论成功');
                  return $this->refresh();
      }
        // 接收到article_id时 渲染具体的live文章页面
      else if($id!=null)
      {	
        $art= Article::getArticleById($id);
        $data = Comment::getCommByArtId($id);
      	return $this->render('article',[
          'article'=>  $art,
          'author'=> User::getUsernameById($art->post_author_id),
          'newArticle'=>Article::getNewArticle("3"),
          'comment_data'=>$data,
          'blogname'=>Options::getOption("blogname"),
           'intro'=>Options::getOption("introduction"),
          'comment'=>$comment,
        ]);
      }
    //没get到id时，渲染文章列表的页面
      else
      {	//$ob为数组，装着两个对象 $pagination和$articles，详情在Article类中，其中10为pagesize
	        $ob = Article::getPaginationArt(6);
	        return $this->render('index',[
	             'articles' => $ob['articles'],
	             'pages' => $ob['pagination'],
                     'newArticle'=>Article::getNewArticle("3"),
                     'blogname'=>Options::getOption("blogname"),
                     'intro'=>Options::getOption("introduction"),
	       ]);}
      }
        public function actionCommentLive()
      {
          $com = Yii::$app->request->get(); 
          if($com!=null)
          {
            $ob_com = new Comment_live();
            $ob_com->live_id = $com['live_Id'];
            $ob_com->comment_date = $com['date'];
            $ob_com->comment_content =  $com['comment_content'];
            // $ob_com->user_id =  0;
            $ob_com->save();
            return "评论成功";
          }
      }

      public function actionLive()
      {
        $id = Yii::$app->request->get('id');  
        $comment_live= new Comment_live(); 
   // 接收评论并验证，true则存入数据库
         if($comment_live->load(\Yii::$app->request->post())&&$comment_live->validate())
     {        
      Yii::trace("test success");
                  $comment_live->comment_date = date("Y-m-d H:i:s",time());
                  $comment_live->live_id = $id;
                  $comment_live->save();    
                  \Yii::$app->session->setFlash('success','评论成功');
                  return $this->refresh();
      }
      // 接收到live_id时 渲染具体的live文章页面
       else if($id!=null)
      { 
        $live = Live::getLiveById($id);
        $data = Comment_live::getCommByLiveId($id);
        return $this->render('live_art',[
          'lives'=>  $live,
          'author'=> User::getUsernameById($live->post_author_id),
          'newLive'=>Live::getNewLive("3"),
          'comment_data'=>$data,
          'blogname'=>Options::getOption("blogname"),
           'intro'=>Options::getOption("introduction"),
           'comment_live'=>$comment_live,
        ]);
      }else{
          //没get到id时，渲染文章列表的页面
       //$ob为数组，装着两个对象 $pagination和$articles，详情在live类中，其中10为pagesize
          $ob = Live::getPaginationLive(6);
          return $this->render('live',[
               'lives' => $ob['lives'],
               'pages' => $ob['pagination'],
                     'newlive'=>Live::getNewLive("3"),
                     'blogname'=>Options::getOption("blogname"),
                     'intro'=>Options::getOption("introduction"),
         ]);}
      }

}
